//
//  PickCarsDetailVC.m
//  Zhiche-Carriage
//
//  Created by 王亚陆 on 16/6/16.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "PickCarsDetailVC.h"
#import <Masonry.h>
#import "Common.h"
#define TimeTag 100
#define TableTag  200
#define SmallTabelTag 300
#define chooseTag 400


@interface PickCarsDetailVC ()
{
    UITableView * table;
    UIView * nav;
    Common * com;
    UIScrollView * scroll;
}
@property (nonatomic,strong) UIView * BackView;
@property (nonatomic,strong) NSMutableArray * headerViewArr;

@end

@implementation PickCarsDetailVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.ID = [[NSString alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    com = [[Common alloc]init];
//    _headerViewArr ＝[];
    self.view.backgroundColor = WhiteColor;
    _headerViewArr = [[NSMutableArray alloc]init];

    nav = [self createNav:@"接单"];
    [self.view addSubview:nav];
    [self createTableView];
    [self createScroll];
    [self createTable];
//    [self createDate];
}

-(void)viewWillAppear:(BOOL)animated{
    
    NSString * string = [NSString stringWithFormat:@"%@?id=%@",extractdetail_Url,self.ID];
    NSLog(@"%@",string);
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
        
        NSLog(@"%@",responseObj);
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        label.text = responseObj[@"message"];
        [self.view addSubview:label];
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];

    

}

-(void)createScroll{
    
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, Main_Width,Main_height-64*kHeight)];
    scroll.contentOffset = CGPointMake(0, 0);
    scroll.contentSize = CGSizeMake(Main_Width, Main_height*1.5);
    self.automaticallyAdjustsScrollViewInsets =NO;
    scroll.bounces = NO;
    //隐藏横向、纵向的滚动条
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.delegate = self;
    scroll.minimumZoomScale = 1.0;
    scroll.maximumZoomScale = 3.0;
    [self.view addSubview:scroll];
    
    [scroll mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(nav.mas_bottom).with.offset(0*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, Main_height-64*kHeight));
    }];
    //设置scrollView的内容视图
    _BackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, Main_height*kHeight*1.5)];
    _BackView.backgroundColor = [UIColor whiteColor];
    [scroll addSubview:_BackView];
}

-(void)createTable{
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 65*kHeight, Main_Width,Main_height-65*kHeight) style:UITableViewStylePlain];
    [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    [table reloadData];
}
-(void)createDate{
    
    int pageNo = 1;
    int pageSize = 10;
    
    NSString * string = nil;
    
    string = [NSString stringWithFormat:@"%@?pageNo=%d&pageSize=%d",task_Url,pageNo,pageSize];
    
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
        
        NSLog(@"%@",responseObj);
        
        [table reloadData];
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
}
-(void)createTableView{
    
    [_headerViewArr removeAllObjects];
    for (int i =0; i<3; i++) {
        
        UIView * SectionView = [[UIView alloc]init];
        SectionView.backgroundColor = [UIColor whiteColor];
        SectionView.tag = 900+i;
        SectionView.frame = CGRectMake(0, 0, Main_Width, 43*kHeight);
        UIImageView * SmallImage = [[UIImageView alloc]init];
        SmallImage.backgroundColor = [UIColor cyanColor];
        UILabel * CarName = [com createUIlabel:@"东风雪铁龙" andFont:FontOfSize14 andColor:BlackColor];
       
        UILabel * Number = [com createUIlabel:@"2" andFont:FontOfSize12 andColor:BlackColor];

        Number.tag = 1000+i;
        
        [SectionView addSubview:SmallImage];
        [SectionView addSubview: CarName];
        [SectionView addSubview: Number];
        
        [SmallImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(SectionView.mas_left).with.offset(14*kWidth);
            make.centerY.mas_equalTo(SectionView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(15*kWidth, 15*kHeight));
        }];
        [CarName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(SmallImage.mas_right).with.offset(14*kWidth);
            make.centerY.mas_equalTo(SmallImage.mas_centerY);
        }];
        [Number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(SectionView.mas_right).with.offset(-14*kWidth);
            make.centerY.mas_equalTo(SmallImage.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(30*kWidth, 30*kHeight));
        }];
        UIView * Hline =[[UIView alloc]init];
        Hline.backgroundColor = RGBACOLOR(149, 149, 149, 1);
        [SectionView addSubview:Hline];
        UIImageView * imageline =[[UIImageView alloc]init];
        imageline.image = [UIImage imageNamed:@"common_list_line1"];
        [SectionView addSubview:imageline];
        [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SectionView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            make.bottom.mas_equalTo(SectionView.mas_bottom);
        }];
        
        [_headerViewArr addObject:SectionView];
    }
}


-(void)createUI:(NSMutableDictionary *)addressDic{

    NSLog(@"%@",addressDic);
//    {
//        arriveDate = "2016-06-22";
//        arriveTime = "<null>";
//        deliveryDate = "2016-06-22";
//        deliveryTime = "<null>";
//        departCityName = "\U5317\U4eac\U5e02";
//        departUnit = "\U5317\U4eac\U77e5\U8f66\U79d1\U6280";
//        id = 130;
//        orderCode = 745549747160285184;
//        receiptCityName = "\U5929\U6d25\U5e02";
//        receiptUnit = "\U53ef\U53e3\U53ef\U4e50\U4e86";
//        status = 45;
//        statusText = "\U5f85\U63d0\U8f66";
//        vehicles =     (
//                        {
//                            amount = 1;
//                            brandName = "\U5965\U8fea";
//                            id = 142;
//                            vehicleName = "\U5965\U8feaA4";
//                        },
//                        {
//                            amount = 1;
//                            brandName = "AC Schnitzer";
//                            id = 143;
//                            vehicleName = "AC Schnitzer X5";
//                        }
//                        );
//    }
//
    NSString * ordercode = [NSString stringWithFormat:@"%@",addressDic[@"orderCode"]];
    UILabel * label = [com createUIlabel:ordercode andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(_BackView.mas_top).with.offset(10*kHeight);
    }];
    
    UILabel * CarModel = [com createUIlabel:@"地址" andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:CarModel];
    [CarModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(label.mas_bottom).with.offset(10*kHeight);
    }];

    UILabel * CityName = [com createUIlabel:addressDic[@"departCityName"] andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:CityName];
    [CityName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(55*kWidth);
        make.top.mas_equalTo(CarModel.mas_bottom).with.offset(10*kHeight);
    }];
    UILabel * StoreName = [com createUIlabel:addressDic[@"departUnit"] andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:StoreName];
    [StoreName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CityName.mas_right).with.offset(20*kWidth);
        make.centerY.mas_equalTo(CityName.mas_centerY);
    }];
    UILabel * AddressName = [com createUIlabel:addressDic[@"receiptCityName"] andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:AddressName];
    [AddressName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(55*kWidth);
        make.top.mas_equalTo(CityName.mas_bottom).with.offset(10*kHeight);
    }];
    UILabel * PeopleName = [com createUIlabel:@"雷老板  123456789" andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:PeopleName];
    [PeopleName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(55*kWidth);
        make.top.mas_equalTo(AddressName.mas_bottom).with.offset(10*kHeight);
    }];
    UIButton * btn = [com createBtn:@"" andFont:FontOfSize14 andTag:1 and:self action:@selector(pressiphone) andTitleColor:BlackColor andBorderColor:nil andBackgroundColor:WhiteColor];
    
    btn.backgroundColor = [UIColor cyanColor];
    [_BackView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(PeopleName.mas_right).with.offset(55*kWidth);
        make.centerY.mas_equalTo(PeopleName.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(17*kWidth, 17*kHeight));
    }];
    UIView * Hline =[[UIView alloc]init];
    Hline.backgroundColor = RGBACOLOR(149, 149, 149, 1);
    [_BackView addSubview:Hline];
    [Hline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left);
        make.top.mas_equalTo(PeopleName.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];
    
    
    UILabel * SCityName = [com createUIlabel:addressDic[@"receiptCityName"] andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:SCityName];
    [SCityName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(55*kWidth);
        make.top.mas_equalTo(Hline.mas_bottom).with.offset(10*kHeight);
    }];
    
    UILabel * SStoreName = [com createUIlabel:addressDic[@"receiptUnit"] andFont:FontOfSize14 andColor:BlackColor];
    [_BackView addSubview:SStoreName];
    [SStoreName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SCityName.mas_right).with.offset(20*kWidth);
        make.centerY.mas_equalTo(SCityName.mas_centerY);
    }];

    
    UILabel * timelabel = [com createUIlabel:@"时间" andFont:14.0 andColor:RGBACOLOR(149, 149, 149, 1)];
    [_BackView addSubview:timelabel];
    [timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(SStoreName.mas_bottom).with.offset(10*kHeight);
    }];
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSString * str1 = [NSString stringWithFormat:@"%@     %@",addressDic[@"deliveryDate"],addressDic[@"deliverytime"]] ;
    [arr addObject:str1];
    
    NSString * str2 = [NSString stringWithFormat:@"%@     %@",addressDic[@"arriveDate"],addressDic[@"arrivetime"]] ;
    [arr addObject:str2];
    
    NSArray * arrTime = @[@"提车时间:",@"送达时间:"];
    for (int i=0; i<arrTime.count; i++) {
        UILabel * time = [com createUIlabel:arrTime[i] andFont:FontOfSize14 andColor:BlackColor];
        [_BackView addSubview:time];
        [time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
            make.top.equalTo(timelabel.mas_bottom).with.offset(10*kHeight+36*kHeight*i);
        }];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:BlackColor forState:UIControlStateNormal];
        btn.tag = TimeTag+i;
        btn.titleLabel.font = Font(FontOfSize14);
        [_BackView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_BackView.mas_centerX);
            make.centerY.equalTo(time.mas_centerY);
        }];
        UIImageView * imageline =[[UIImageView alloc]init];
        imageline.image = [UIImage imageNamed:@"common_list_line1"];
        
        if(i==1){
            imageline.tag = 50;
        }
        [_BackView addSubview:imageline];
        [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_BackView.mas_left).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            make.top.mas_equalTo(time.mas_bottom).with.offset(10*kHeight);
        }];
    }
    
    UIImageView * imageLine = (UIImageView*)[_BackView viewWithTag:50];
    UILabel * carModel = [com createUIlabel:@"车型" andFont:14.0 andColor:RGBACOLOR(149, 149, 149, 1)];
    [_BackView addSubview:carModel];
    [carModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(imageLine.mas_bottom).with.offset(10*kHeight);
    }];
    NSInteger number = [addressDic[@"vehicles"] count];
    
    NSString * Number = [NSString stringWithFormat:@"合计:%ld台",number];
    UILabel * amount =[com createUIlabel:Number andFont:FontOfSize14 andColor: BlackColor];
    [_BackView addSubview:amount];
    [amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_BackView.mas_right).with.offset(-14*kWidth);
        make.centerY.mas_equalTo(carModel.mas_centerY);
    }];
    
    
    table.backgroundColor = [UIColor cyanColor];
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_BackView.mas_left).with.offset(0);
        make.top.mas_equalTo(carModel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 45*(3)*kHeight));
    }];
    
    UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100*kWidth, 30*kHeight)];
    comment.text = @"备注";
    comment.font = Font(12);
    comment.textColor = RGBACOLOR(149, 149, 149, 1);
    [_BackView addSubview:comment];
    [comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(table.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(200, 25));
    }];
    
    NSString * commentLabel = [NSString stringWithFormat:@"%@",addressDic[@"comment"]];
    UILabel * field =[com createUIlabel:commentLabel andFont:FontOfSize14 andColor:BlackColor];
    
    [_BackView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(14*kWidth);
        make.top.mas_equalTo(comment.mas_bottom).with.offset(5*kHeight);
    }];
    
    UIView * Hline1 =[[UIView alloc]init];
    Hline1.backgroundColor = RGBACOLOR(149, 149, 149, 1);
    [_BackView addSubview:Hline1];
    [Hline1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_BackView.mas_centerX);
        make.top.mas_equalTo(field.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];
    
    UIButton * ShipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ShipBtn setTitle:@"我要报价" forState:UIControlStateNormal];
    [ShipBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    [ShipBtn addTarget:self action:@selector(pressShipBtn) forControlEvents:UIControlEventTouchUpInside];
    ShipBtn.layer.borderWidth = 1;
    ShipBtn.layer.borderColor = RGBACOLOR(149, 149, 149, 1).CGColor;
    ShipBtn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:ShipBtn];
    [ShipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(Main_Width/3*2, 35*kHeight));
        make.top.mas_equalTo(Hline1.mas_bottom).with.offset(30*kHeight);
    }];
}


-(void)pressiphone{
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://15011578178"]]];
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str=@"str";
    
    UITableViewCell * cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor cyanColor];
    cell.textLabel.text = @"123456789";
    return cell;
    
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (_headerViewArr.count>0) {
            return _headerViewArr[section];
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 43*kHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 37*kHeight;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return 3;
}
//设置分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_headerViewArr.count>0) {
        return _headerViewArr.count;
    }
    return 1;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
